<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('总费用管理')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <th:block th:include="include :: select2-css" />
    <style>
        .dropdown-menu-center{
            min-width: 100%;
            background-color: rgba(0,0,0,0.4);
            color: white;
            /*font-weight:bolder;*/
            /*font-style: italic;*/
            /*font-family: "楷体";*/
        }
    </style>
</head>
<body class="gray-bg">

<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="meta-table-form">
                    <input type="hidden" id="tableId" name="tableId">
                    <div class="select-list">
                        <ul>
                            <li>
                                日期：
                                <input type="text" placeholder="请填写日期" id="etlDt" name="etlDt"/>
                            </li>
                            <li>
                                楼层：<select id="floorName" name="floorName" class="form-control">
                                <option value="">所有</option>
                                <option th:each="floorInfo: ${floorInfos}" th:text="${floorInfo.floorName}" th:value="${floorInfo.floorName}"></option>
                            </select>
                            </li>
                            <li>
                                租户：<select id="tenantName" name="tenantName" class="form-control">
                                <option value="">所有</option>
                                <option th:each="tenantInfo: ${tenantInfos}" th:text="${tenantInfo.tenantName}" th:value="${tenantInfo.tenantName}"></option>
                            </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="query()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn  btn-info" onclick="createTotalFee()" shiro:hasPermission="totalFee:create">
                    <i class="fa fa-remove"></i> 计算
                </a>
                <a class="btn btn-info" onclick="importTotalFeeExcel()" shiro:hasPermission="totalFee:import">
                    <i class="fa fa-upload"></i> 总费用导入
                </a>
                <a class="btn btn-warning" onclick="exportTotalFeeExcel()" shiro:hasPermission="totalFee:export">
                    <i class="fa fa-download"></i> 总费用导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer" />
<th:block th:include="include :: layout-latest-js" />
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-select-js" />

<script th:inline="javascript">
    var prefix = ctx + "sujian/totalCostManagement";

    // $(function() {
    //     initable();
    // });



    function initSelect() {
        $("select[name='floor_name']").select2({
            placeholder: "--请选择楼层--",
            allowClear:true
        });
        $("select[name='tenant_name']").select2({
            placeholder: "--请选择单位--",
            allowClear:true
        });
    }
    var options;
    function query() {
        if(options!=null&&options!=undefined){
            var params = $("#bootstrap-table").bootstrapTable('getOptions');
            $("#bootstrap-table").bootstrapTable('refresh', params);
        }else{
            options = {
                url: prefix + "/list",
                modalName: "总费用",
                striped:true,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'uuid',
                        title: '表uuid号',
                        visible: false
                    },
                    {
                        field: 'floorName',
                        title: '楼层'
                    },
                    {
                        field: 'tenantName',
                        title: '单位名称'
                    },
                    {
                        field: 'tenantArea',
                        title: '建筑面积M2'
                    },
                    {
                        field: 'wgFee',
                        title: '物管费'
                    },
                    {
                        field: 'waterNum',
                        title: '水表数'
                    },
                    {
                        field: 'waterFee',
                        title: '水费'
                    },
                    {
                        field: 'electricNum',
                        title: '电表数'
                    },
                    {
                        field: 'zhaomingNum',
                        title: '实际用电'
                    },
                    {
                        field: 'zhaomingFee',
                        title: '照明费'
                    },
                    {
                        field: 'dongliNum',
                        title: '动力用电'
                    },
                    {
                        field: 'dongliFee',
                        title: '动力费'
                    },
                    {
                        field: 'gongyongNum',
                        title: '公共电'
                    },
                    {
                        field: 'gongyongFee',
                        title: '公用电费'
                    },
                    {
                        field: 'kongtiaoNum',
                        title: '空调电量'
                    },
                    {
                        field: 'kongtiaoFee',
                        title: '空调电费'
                    },
                    {
                        field: 'dianfeiTotal',
                        title: '电费小计'
                    },
                    {
                        field: 'kongtiaoOilFee',
                        title: '空调油费'
                    },
                    {
                        field: 'oilTotal',
                        title: '油费合计'
                    },
                    {
                        field: 'feeTotal',
                        title: '合计'
                    },
                    {
                        field: 'sjfq',
                        title: '日期'
                    },
                    {
                        field: 'lastModifyTime',
                        title: '更新时间'
                    }
                ]
            };
            $.table.init(options);
        }
    }

    function exportTotalFeeExcel(){
        var etlDate = $("#etlDt").val();
        if (null == etlDate || "undefined" == etlDate || "" == etlDate) {
            $.modal.msgError("请在页面选择日期，确定导出哪个年月的总费用信息。");
            return
        }
        var yearMonth = etlDate.replaceAll("-","");
        var data = $('#meta-table-form').serializeArray();
        data.push({"name":"etlDt","value":yearMonth});
        $.ajax({
            url: prefix + "/exportTotalFee",
            type: "post",
            dataType: "json",
            data: data,
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
                $.modal.disable();
            },
            success: function (result) {
                if(result.code==web_status.SUCCESS&&result.data!=""){
                    window.location.href = prefix+"/downloadTotalFeeExcel/"+result.data;
                    $.modal.closeAll();
                    $.modal.closeLoading();
                }else{
                    $.modal.closeLoading();
                    $.modal.alertError(result.msg)
                    $.table.refresh();
                }
            }
        })
    }

    function importTotalFeeExcel() {
        var etlDate = $("#etlDt").val();
        if (null == etlDate || "undefined" == etlDate || "" == etlDate) {
            $.modal.msgError("请在页面选择日期，确定导入哪个年月的总费用信息。");
            return
        }
        var yearMonth = etlDate.replaceAll("-","").substring(0,6);
        var year = yearMonth.substring(0,4);
        var month = yearMonth.substring(4,6);
        layer.open({
            type: 1,
            area: ['600px', '400px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: '导入' + year +'年' + month + '月的' + table.options.modalName + '数据',
            content: $('#importAddTpl').html(),
            btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
            // 弹层外区域关闭
            shadeClose: true,
            btn1: function(index, layero){
                var file = layero.find('#addFile').val();
                if (file == '' || (!$.common.endWith(file, '.xlsx'))){
                    $.modal.msgWarning("请选择后缀为“xlsx”的文件。");
                    return false;
                }
                $.modal.disable();
                var formData = new FormData(layero.find('form')[0]);
                formData.append("etldt",yearMonth);
                $.ajax({
                    url: prefix + "/importTotalFeeData",
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    type: 'POST',
                    success: function (result) {
                        if (result.code == web_status.SUCCESS) {
                            $.modal.closeAll();
                            $.modal.alertSuccess(result.msg);
                            $.table.refresh();
                        } else if (result.code == web_status.WARNING) {
                            layer.close(index);
                            $.modal.enable();
                            $.modal.alertWarning(result.msg)
                        } else {
                            layer.close(index);
                            $.modal.enable();
                            $.modal.alertError(result.msg);
                        }
                    }
                });
            }
        });
    }

    function importTotalFeeTemplate(){
        $.get(prefix + "/importTotalFeeTemplate", function(result) {
            if (result.code == web_status.SUCCESS) {
                window.location.href = ctx + "common/download?fileName=" + encodeURI(result.data) + "&delete=" + true;
            } else if (result.code == web_status.WARNING) {
                $.modal.alertWarning(result.msg)
            } else {
                $.modal.alertError(result.msg);
            }
        });
    }

    function createTotalFee(){
        layer.open({
            type:1,
            title:["选择计算总费用的月份","font-size:18px"],
            area:["400px","180px"],
            shade:false,
            anim:3,
            shade:0.3,
            content:$("#etldtArea").html(),
            btn:['确认'],
            yes:function (index) {
                var etldt = $('#generateEtldt').val();
                if(etldt==null||etldt==""||etldt==undefined){
                    $.modal.alertError("请输入计算总费用的月份")
                    return;
                }
                var yearMonth = etldt.replaceAll("-","");
                var data = { "etlDt": yearMonth };
                $.ajax({
                    url: prefix + "/createTotalFee",
                    type: "post",
                    dataType: "json",
                    data: data,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    error : function(request) {
                        $.modal.alertError("系统错误");
                    },
                    success : function(data) {
                        $.operate.successCallback(data);
                    }
                })
            }
        })
    }
</script>
</body>
<script id="importAddTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb20">
        <div class="col-xs-offset-1">
            <input type="file" id="addFile" name="file"/>
            <div class="mt20 pt20">
                <a onclick="importTotalFeeTemplate()" class="btn btn-default btn-xs" shiro:hasPermission="totalFee:template"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <span color="red" class="pull-left mt20">
                提示：仅允许导入“xlsx”格式文件！
            </span>
        </div>
    </form>
</script>
<script type="text/template" id="etldtArea">
    <form class="mt20 mb10">
        <div style="margin: 20px;">
            <span style="color: red;">若选择月份已有总费用记录，将直接覆盖！</span>
            <input type="text" class="form-control" id="generateEtldt" placeholder="请输入要计算总电费的月份（yyyyMM）">
        </div>
    </form>
</script>
</html>